//
//  Label.swift
//  UITest
//
//  Created by YJH on 2025/5/15.
//

import UIKit

class Label: UIViewController {
//    // MARK: - 接收传递的标题
//    var selectedTitle: String? // 用于接收选中的控件名称
    
//     MARK: - UILabel静态动态样式
        private var staticLabel: UILabel!
        private var dynamicLabel: UILabel!
        private var styledLabel: UILabel!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = .white
//            
//            // 使用传递的标题更新导航栏
//            title = selectedTitle
    
            setupUI()
            updateDynamicLabel()
        }
    
        private func setupUI() {
            // 1. 基础静态标签
            staticLabel = UILabel(frame: CGRect(x: 50, y: 100, width: view.bounds.width - 100, height: 40))
            staticLabel.text = "这是一个静态标签"
            staticLabel.textAlignment = .center
            staticLabel.textColor = .black
            view.addSubview(staticLabel)
//            staticLabel.transform = CGAffineTransform(scaleX: 2, y: 0.5)
//            staticLabel.transform = CGAffineTransform(translationX: 50, y: 0)
//            staticLabel.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
    
            // 2. 动态更新的标签
            dynamicLabel = UILabel(frame: CGRect(x: 50, y: 160, width: view.bounds.width - 100, height: 40))
            dynamicLabel.textAlignment = .center
            dynamicLabel.textColor = .systemBlue
            view.addSubview(dynamicLabel)
    
            // 3. 带样式的标签
            styledLabel = UILabel(frame: CGRect(x: 50, y: 220, width: view.bounds.width - 100, height: 100))
            styledLabel.text = "这是一个带样式的标签\n支持多行显示\n自定义字体和颜色"
            styledLabel.textAlignment = .center
            styledLabel.textColor = .systemRed
            styledLabel.font = UIFont.systemFont(ofSize: 18, weight: .bold)
            styledLabel.numberOfLines = 2 // 允许多行
            styledLabel.backgroundColor = .systemYellow.withAlphaComponent(0.3)
            styledLabel.layer.cornerRadius = 8
            styledLabel.layer.masksToBounds = true
            view.addSubview(styledLabel)
    
            // 添加按钮用于更新标签
            let updateButton = UIButton(type: .system)
            updateButton.frame = CGRect(x: 50, y: 340, width: view.bounds.width - 100, height: 40)
            updateButton.setTitle("更新动态标签", for: .normal)
            updateButton.addTarget(self, action: #selector(updateButtonTapped), for: .touchUpInside)
            view.addSubview(updateButton)
        }
    
        private func updateDynamicLabel() {
            let dateFormatter = DateFormatter()
            dateFormatter.dateStyle = .medium
            dateFormatter.timeStyle = .short
            let currentDate = dateFormatter.string(from: Date())
    
            dynamicLabel.text = "当前时间: \(currentDate)"
        }
    
        @objc private func updateButtonTapped() {
            updateDynamicLabel()

//            UIView.animate(withDuration: 1, delay: 0, options: [.autoreverse, .repeat]) {
//                self.dynamicLabel.transform = CGAffineTransform(scaleX: 2, y: 2)
//            }
//            
//            return ()
            
            // 添加一个简单的动画效果
            UIView.animate(withDuration: 0.3) {
                self.dynamicLabel.transform = CGAffineTransform(scaleX: 2, y: 2)
            } completion: { _ in
                UIView.animate(withDuration: 0.3) {
                    self.dynamicLabel.transform = .identity
                }
            }
        }
}
